<template>
	<scroll-view class="stats-page-container" :style="{ paddingTop: safeDistance + 'px' }" scroll-y enable-flex>
		<!-- 页面标题容器 - 模仿index页面的user-overview-container -->
		<view class="stats-header-container">
			<view class="page-header">
				<text class="page-title">📊 任务完成统计</text>
				<text class="page-subtitle">查看您的任务完成趋势</text>
			</view>
		</view>

		<!-- 主要内容区域 - 模仿index页面的task-content -->
		<view class="stats-content">
			<!-- 图表卡片 -->
			<view class="chart-card">
				<view class="card-header">
					<text class="card-title">近期完成量</text>
				</view>
				<view class="charts-box">
					<qiun-data-charts type="column" :opts="opts" :chartData="chartData" />
				</view>
			</view>

			<!-- 统计信息 -->
			<view class="stats-info">
				<Stats />
			</view>
		</view>

		<!-- 底部空间 -->
		<view class="bottom-space"></view>
	</scroll-view>
</template>

<script setup lang="ts">
import { onMounted, ref, computed } from 'vue';
import { listHistoryStatsAPI } from '@/services/task';
import { HistoryStatsItem } from '@/types';
import { getThemeStyles } from '@/stores/theme';
import qiunDataCharts from '@/uni_modules/qiun-data-charts/components/qiun-data-charts/qiun-data-charts';
import Stats from './components/stats.vue';

/**
* 安全距离（顶部内边距 = 安全区域 + 基础间距）
*/
const safeDistance = computed(() => {
	const systemInfo = uni.getSystemInfoSync();
	const safeAreaTop = systemInfo.safeAreaInsets?.top || 0;
	// 基础间距 20px（约等于 40rpx），加上安全区域
	return safeAreaTop + 20;
});
/**
 * 获取当前主题样式
 */
const themeStyles = computed(() => getThemeStyles());

// 定义图表数据类型
interface ChartSeries {
	name: string;
	data: number[];
}

interface ChartData {
	categories: string[];
	series: ChartSeries[];
}

// 图表数据
const chartData = ref<ChartData>({} as ChartData);

// 图表配置选项
const opts = {
	color: ["#1890FF", "#91CB74", "#FAC858", "#EE6666", "#73C0DE", "#3CA272", "#FC8452", "#9A60B4", "#ea7ccc"],
	padding: [15, 15, 0, 5],
	enableScroll: false,
	legend: {},
	xAxis: {
		disableGrid: true
	},
	yAxis: {
		data: [
			{
				min: 0
			}
		]
	},
	extra: {
		column: {
			type: "group",
			width: 30,
			activeBgColor: "#000000",
			activeBgOpacity: 0.08,
			linearType: "custom",
			seriesGap: 5,
			linearOpacity: 0.5,
			barBorderCircle: true,
			customColor: [
				"#FA7D8D",
				"#EB88E2"
			]
		}
	}
};




// 获取服务器数据
const getServerData = (): void => {
	let response = listHistoryStatsAPI();
	response.then(res => {
		let historyStatsItem: HistoryStatsItem = res.data;
		chartData.value = {
			categories: historyStatsItem.completeDate.map(date => date.slice(5, 10)),
			series: [
				{
					name: "完成量",
					data: historyStatsItem.completeCount
				}
			]
		}
	})


};

// 组件挂载完成后执行
onMounted(() => {
	getServerData();
});
</script>

<style lang="scss">
/**
	 * 统计页面容器 - 模仿index页面的task-app-container
	 */
.stats-page-container {
	height: 100vh;
	background-color: v-bind('themeStyles.bgPrimary');
}

/**
	 * 统计标题容器 - 模仿index页面的user-overview-container
	 */
.stats-header-container {
	border-radius: 6px;
	border: 1px solid v-bind('themeStyles.borderLight');
	margin: 10px;
	box-shadow: v-bind('themeStyles.cardBoxShadow');
	background: v-bind('themeStyles.primaryGradient');
}

/**
	 * 页面标题 - 内部样式
	 */
.page-header {
	padding: 32rpx 20rpx;
	text-align: center;
}

@keyframes headerFadeIn {
	from {
		opacity: 0;
		transform: translateY(-20rpx);
	}

	to {
		opacity: 1;
		transform: translateY(0);
	}
}

.page-title {
	display: block;
	font-size: 36rpx;
	font-weight: 700;
	color: white;
	margin-bottom: 8rpx;
	letter-spacing: 1rpx;
}

.page-subtitle {
	display: block;
	font-size: 24rpx;
	color: rgba(255, 255, 255, 0.9);
	font-weight: 400;
}

/**
	 * 主要内容区域 - 模仿index页面的task-content
	 */
.stats-content {
	display: flex;
	flex-direction: column;
	padding: 24rpx;
	min-height: 100%;
	margin: 20rpx;
	border-radius: 16rpx;
	border: 1rpx solid v-bind('themeStyles.borderLight');
	background: v-bind('themeStyles.bgCard');
	box-shadow: v-bind('themeStyles.cardBoxShadow');
	position: relative;
	overflow: hidden;

	&::before {
		content: '';
		position: absolute;
		top: 0;
		left: 0;
		right: 0;
		height: 4rpx;
		background: v-bind('themeStyles.primaryGradient');
	}
}

/**
	 * 图表卡片
	 */
.chart-card {
	background: transparent;
	border-radius: 16rpx;
	padding: 0;
	border: none;
	box-shadow: none;
	margin-bottom: 24rpx;
}

@keyframes cardFadeIn {
	from {
		opacity: 0;
		transform: translateY(20rpx);
	}

	to {
		opacity: 1;
		transform: translateY(0);
	}
}

/**
	 * 卡片标题
	 */
.card-header {
	margin-bottom: 20rpx;
	padding-bottom: 16rpx;
	border-bottom: 1rpx solid v-bind('themeStyles.borderLight');
}

.card-title {
	font-size: 28rpx;
	font-weight: 600;
	color: v-bind('themeStyles.textPrimary');
}

/**
	 * 图表容器 - 保持原有尺寸
	 */
.charts-box {
	width: 100%;
	height: 300px;
}

/**
	 * 统计信息区域
	 */
.stats-info {
	margin-top: 24rpx;
}

/**
	 * 底部空间
	 */
.bottom-space {
	height: 100rpx;
}
</style>